package com.openlogic.logic.controller.logic;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Get;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Param;
import org.noear.solon.auth.annotation.AuthPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.openlogic.api.logic.dto.RuleFuncLibDto;
import com.openlogic.common.core.enums.BusinessType;
import com.openlogic.common.core.web.controller.BaseController;
import com.openlogic.common.core.web.domain.PageData;
import com.openlogic.common.datasource.annotation.QzLog;
import com.openlogic.common.logic.utils.FuncXmlParseUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
* @ClassName: RuleFuncController
* @Description: 决策函数库Controller
* @author weiyongpeng
* @date 2024年7月22日 下午3:45:05
 */
@Api(value = "决策函数库管理功能API")
@Controller
@Mapping("rule/func")
public class RuleFuncController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(RuleFuncController.class);
	
	/**
	* 描述：决策流函数列表
	* @Title: listFuncLib
	* @param params
	* @return
	* @author weiyongpeng
	* @date  2024年9月27日 下午4:30:15
	 */
	@ApiOperation(value = "决策函数库查询")
	@Get
	@Mapping("listFuncLib")
	@AuthPermissions("rule:func:list")
	@QzLog(title = "决策函数库查询", businessType = BusinessType.LIST)
	public PageData<Map<String,RuleFuncLibDto>> listFuncLib(@Param Map<String, Object> params) {
		try {
			InputStream cusIs = RuleFuncController.class.getClassLoader().getResource("customExper-config.xml").openStream();
			File file = File.createTempFile("customExper-config","xml");
			FileUtils.copyInputStreamToFile(cusIs, file);
			List<Map<String,RuleFuncLibDto>> list = FuncXmlParseUtils.getFuncLibList(file.getAbsolutePath());
			file.delete();
			return getPageData(list);
		} catch (Exception e) {
			logger.error("获取规则函数库异常:{}",e);
			// throw new ServiceException("获取规则函数库异常");
		}
		return getPageData(new ArrayList<>());
	}
	
	/**
	* 描述：决策流函数列表
	* @Title: listFuncLib
	* @param params
	* @return
	* @author weiyongpeng
	* @date  2024年9月27日 下午4:30:15
	 */
	@ApiOperation(value = "考核方案指标参数决策函数库查询")
	@Get
	@Mapping("listEvalmngRuleLib")
	@AuthPermissions("rule:func:list")
	@QzLog(title = "决策函数库查询", businessType = BusinessType.LIST)
	public PageData<Map<String,RuleFuncLibDto>> listEvalmngRuleLib(@Param Map<String, Object> params) {
		try {
			InputStream cusIs = RuleFuncController.class.getClassLoader().getResource("aviatorFunc-config.xml").openStream();
			File file = File.createTempFile("aviatorFunc-config","xml");
			FileUtils.copyInputStreamToFile(cusIs, file);
			List<Map<String,RuleFuncLibDto>> list = FuncXmlParseUtils.getFuncLibList(file.getAbsolutePath());
			file.delete();
			return getPageData(list);
		} catch (Exception e) {
			logger.error("获取规则函数库异常:{}",e);
			// throw new ServiceException("获取规则函数库异常");
		}
		return getPageData(new ArrayList<>());
	}
	
	/**
	* 描述：决策树函数库列表
	* @Title: listDecisionTreeFuncLib
	* @param params
	* @return
	* @author weiyongpeng
	* @date  2024年9月27日 下午4:44:38
	 */
	@ApiOperation(value = "决策树函数库查询")
	@Get
	@Mapping("listDecisionTreeFuncLib")
	@AuthPermissions("rule:func:list")
	@QzLog(title = "决策树函数库查询", businessType = BusinessType.LIST)
	public PageData<Map<String,RuleFuncLibDto>> listDecisionTreeFuncLib(@Param Map<String, Object> params) {
		try {
			InputStream cusIs = RuleFuncController.class.getClassLoader().getResource("customExper-config.xml").openStream();
			File file = File.createTempFile("customExper-config","xml");
			FileUtils.copyInputStreamToFile(cusIs, file);
			List<Map<String,RuleFuncLibDto>> list = FuncXmlParseUtils.getFuncLibList(file.getAbsolutePath());
			file.delete();
			return getPageData(list);
		} catch (Exception e) {
			logger.error("获取规则函数库异常:{}",e);
			// throw new ServiceException("获取规则函数库异常");
		}
		return getPageData(new ArrayList<>());
	}

}
